package com.security.brows.ui

import android.content.Context
import android.content.Intent
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import com.security.brows.base.SecurityBaseActivity
import com.security.brows.values.SecurityBrowsColorsScheme
import com.security.brows.values.SecurityBrowsDrawable

class SecurityBrowsGuideActivity : SecurityBaseActivity() {
    companion object {
        fun start(context: Context, forceShown: Boolean = true) {
            if (!forceShown) {
                val sp = context.getSharedPreferences("guide_", Context.MODE_PRIVATE)
                val h = sp.getBoolean("has_shown_guide", false)
                if (h) {
                    return
                }
                sp.edit().putBoolean("has_shown_guide", true).apply()
            }
            context.startActivity(Intent(context, SecurityBrowsGuideActivity::class.java))
        }
    }



    @Composable
    override fun ComposeContent(isDarkTheme: Boolean, colorsScheme: SecurityBrowsColorsScheme, drawableScheme: SecurityBrowsDrawable) {
        val guideImageResList = arrayListOf(
            drawableScheme.guide_1,
            drawableScheme.guide_2,
            drawableScheme.guide_3,
            drawableScheme.guide_4
        )
        val guideIndex = remember {
            mutableStateOf(0)
        }
        Box(modifier = Modifier.fillMaxSize()) {
            val redRes = try {
                guideImageResList.get(guideIndex.value)
            } catch (e: Exception) {
                null
            }
            if (redRes != null) {
                Image(
                    painter = painterResource(id = redRes),
                    contentDescription = null,
                    modifier = Modifier
                        .fillMaxSize()
                        .clickable(interactionSource = remember {
                            MutableInteractionSource()
                        }, indication = rememberRipple()) {
                            val nextGuide = guideIndex.value + 1
                            if (nextGuide >= guideImageResList.size) {
                                finish()
                            } else {
                                guideIndex.value = nextGuide
                            }
                        },
                    contentScale = ContentScale.FillBounds
                )
            }
        }
    }
}